package com.yimi.yinhepda.db.dao;

import android.util.Log;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.yimi.yinhepda.base.BaseApplication;
import com.yimi.yinhepda.db.helper.DbHelper;
import com.yimi.yinhepda.entity.message.UserReadMessageBean;
import java.sql.SQLException;
import java.util.List;

/**
 * @author 000432
 * @date 2017/12/19 0019
 */
public class UserReadMessageDao extends BaseDao<UserReadMessageBean, Long> {

  public UserReadMessageDao() {
    super(DbHelper.getInstance(BaseApplication.getContext()), UserReadMessageBean.class);
  }

  public boolean exitReadMessage(String operator, String notfyId) {
    try {
      UserReadMessageBean bean = dao.queryBuilder().where().eq("operator", operator).and().eq("notification_id", notfyId).queryForFirst();
      if (bean == null) {
        return false;
      }
      return true;
    } catch (SQLException e) {
      Log.e(getClass().getSimpleName(), e.getMessage());
      return false;
    }
  }

  public List<UserReadMessageBean> getReadMessageByOperator(String operator) {
    QueryBuilder<UserReadMessageBean, Long> mQueryBuilder = dao.queryBuilder();
    try {
      Where<UserReadMessageBean, Long> where = mQueryBuilder.where();
      where.eq("operator", operator);
      return mQueryBuilder.query();
    } catch (SQLException e) {
      e.printStackTrace();
      return null;
    }
  }

  /**
   * 删除前三个月的数据
   */
  public void deleteThreeMonthData(String oldDate) {
    QueryBuilder<UserReadMessageBean, Long> mQueryBuilder = dao.queryBuilder();
    try {
      Where<UserReadMessageBean, Long> where = mQueryBuilder.where();
      where.lt("create_time", oldDate);
      List<UserReadMessageBean> lists = mQueryBuilder.query();
      if (lists != null && lists.size() > 0) {
        delete(lists);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
